package pl.asoft.nbp.dao.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import pl.asoft.nbp.dao.CurrencyDAO;
import pl.asoft.nbp.model.Currency;

import java.util.List;

/**
 * Created by rbiedrawa on 2014-12-09.
 */

@Repository("currencyDAO")
@Transactional
public class CurrencyDAOImpl implements CurrencyDAO {

    @Autowired
    protected SessionFactory sf;

    @Override
    public Currency saveOrUpdate(Currency currency) {
        sf.getCurrentSession().saveOrUpdate(currency);
        return currency;
    }

    @Override
    @Transactional(readOnly = true)
    public Currency find(final String shortCode) {
       return (Currency) sf.getCurrentSession().get(Currency.class,shortCode);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Currency> findAll() {
        return sf.getCurrentSession().createCriteria(Currency.class).list();
    }

    @Override
    public Currency update(final Currency entity) {
        sf.getCurrentSession().update(entity);
        sf.getCurrentSession().flush();
        sf.getCurrentSession().refresh(entity);
        return entity;
    }

    @Override
    public void delete(String shortCode) {
        Currency result = find(shortCode);
        sf.getCurrentSession().delete(result);
    }
    
    @Override
    public Currency save(Currency currency) {
        sf.getCurrentSession().save(currency);
        return currency;
    }
}
